package ms.core.gateway.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ms.core.common.Page;
import ms.core.common.utils.UploadedFileUtils.FileDoc;
import ms.core.db.mysql.QueryParam;
import ms.core.gateway.bean.SysCorpBean;
import ms.core.gateway.request.CreateCorpRequest;
import ms.core.gateway.request.QueryCorpRequest;
import ms.core.gateway.request.UpdateCorpRequest;
import ms.core.gateway.response.SysCorpVO;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.SysTool;

public class SysCorpDao extends BaseDao{

	public SysCorpDao() { super("sys_corp"); }

	/**
	 * 注册企业信息
	 * 
	 * @param ctype
	 * @param corpName
	 * @param linkMan
	 * @param linkTel
	 * @return
	 */
	public String registerCorp(int ctype, String corpName, String linkMan, String linkTel) {
		String uid = SysTool.makeUUID("");
		Map<String, Object> values = new HashMap<>();
		values.put("uid", uid);
		values.put("ctype", ctype);
		values.put("corpName", corpName);
		values.put("linkMan", linkMan);
		values.put("linkTel", linkTel);
		values.put("createTime", new Date());
		values.put("state", SysCorpBean.STATE_NORMAL);

		db.insertRecord(table, values);
		return uid;
	}

	/**
	 * 新增企业信息
	 * 
	 * @param corp
	 * @return
	 */
	public String newCorp(CreateCorpRequest corp) {
		String uid = SysTool.makeUUID("");
		
		Map<String, Object> values = JsonTool.objectToMap(corp, true);
		values.put("uid", uid);
		// ext为对象，需要转字符串处理
		values.put("ext", JsonTool.objectToJson(corp.getExt()));
		values.put("state", SysCorpBean.STATE_NORMAL);
		values.put("createTime", new Date());

		db.insertRecord(table, values);
		return uid;
	}

	public int updateCorp(long id, UpdateCorpRequest corp) {
		Map<String, Object> cdt = new HashMap<>();
		cdt.put("id", id);

		Map<String, Object> values = JsonTool.objectToMap(corp, false);
		// ext为对象，需要转字符串处理
		values.put("ext", JsonTool.objectToJson(corp.getExt()));
		// 更新时间
		values.put("updateTime", new Date());

		return db.modifyRecord(table, values, cdt);
	}
	
	/**
	 * 更新企业资料目录
	 * 
	 * @param id
	 * @param docTypes
	 * @param files
	 */
	public void updateCorpDocType(long id, List<String> docTypes, List<FileDoc> files) {
		Map<String, Object> cdt = new HashMap<>();
		cdt.put("id", id);

		Map<String, Object> values = new HashMap<>();
		values.put("docType", JsonTool.listBeanToJson(docTypes));
		if (files!=null) values.put("docs", JsonTool.listBeanToJson(files));

		db.modifyRecord(table, values, cdt);
	}

	/**
	 * 更新企业附件
	 * 
	 * @param id
	 * @param list
	 */
	public void updateCorpDoc(long id, List<FileDoc> list) {
		Map<String, Object> cdt = new HashMap<>();
		cdt.put("id", id);

		Map<String, Object> values = new HashMap<>();
		values.put("docs", JsonTool.listBeanToJson(list));
		values.put("updateTime", new Date());

		db.modifyRecord(table, values, cdt);
	}

	public <T> T getCorp(String corpUid, Class<T> cls) {
		String sql = String.format("select * from %s where uid=?", table);
		return db.queryForBean(sql, cls, new Object[] {corpUid});
	}

	public <T> T getCorp(int ctype, String corpName, Class<T> cls) {
		List<Object> params = new ArrayList<>();
		String sql = String.format("select * from %s where corpName=?", table);
		params.add(corpName);
		
		if (ctype>0) {
			sql += " and (ctype & ?)>0";
			params.add(ctype);
		}
		return db.queryForBean(sql, cls, params);
	}
	
	/**
	 * 按企业类型和名称模糊查询
	 * 
	 * @param ctype
	 * @param corpName
	 * @return
	 */
	public List<SysCorpVO> queryCorp(int ctype, String corpName) {
		List<Object> param = new ArrayList<>();
		String sql = "select * from "+table+" where 1>0";
		if (ctype>=0) {
			sql += " and (ctype & ?)>0";
			param.add(ctype);
		}
		if (!StrTool.isBlank(corpName)) {
			sql += " and corpName like ?";
			param.add("%"+corpName+"%");
		}
		
		return db.queryForBeans(sql, SysCorpVO.class, param);
	}

	private String getCondition(QueryCorpRequest req, QueryParam qp){
		String ret = "";

		if (!StrTool.isBlank(req.getQ())) {
			ret += " and (corpName like ? or creditCode=? or linkMan=?)";
			qp.addParam("%"+req.getQ()+"%");
			qp.addParam(req.getQ());
			qp.addParam(req.getQ());
		}
		if (req.getCtype()>=0) {
			ret += " and (ctype & ?)>0";
			qp.addParam(req.getCtype());
		}
		if (req.getState()>=0) {
			ret += " and state=?";
			qp.addParam(req.getState());
		}

		return ret;
	}

	/**
	 * 企业查询
	 * 
	 * @param req
	 * @return
	 */
	public Page<SysCorpVO> queryCorp(QueryCorpRequest req) {
		QueryParam qp = QueryParam.getInstance().setPage(req.getPage(), req.getPagesize());
		String sqlWhere = this.getCondition(req, qp);
		String countSql = String.format("select count(*) from %s where 1>0", table) + sqlWhere;
		String querySql = String.format("select * from %s where 1>0", table) + sqlWhere + " order by id";
		
		qp.setCntSql(countSql).setQrySql(querySql);
		return db.queryForPage(qp, SysCorpVO.class);
	}
}
